package com.ruoyi.web.controller.commodity;


import com.ruoyi.commodity.domain.Commodity;
import com.ruoyi.commodity.service.ICommodityService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController extends BaseController {
    private String prefix = "commodity";
    @Autowired
    private ICommodityService commodityService;
    @RequiresPermissions("business:commodity:view")
    @GetMapping()
    public String operlog() {
        return prefix + "/list";
    }

    @RequiresPermissions("business:commodity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Commodity customer) {
        startPage();
        List<Commodity> list = commodityService.selectCommodityList(customer);
        return getDataTable(list);
    }

    @RequiresPermissions("business:commodity:remove")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(commodityService.deleteCommodityByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("business:commodity:add")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file,
                              String coding,
                              String name,
                              String costPrice,
                              String sellingPrice,
                              String brand) throws IOException {
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        Commodity commodity=new Commodity();
        commodity.setBrand(brand);
        commodity.setName(name);
        commodity.setCoding(coding);
        commodity.setPicture(fileName);
        commodity.setCostPrice(new BigDecimal(costPrice));
        commodity.setSellingPrice(new BigDecimal(sellingPrice));
        return toAjax(commodityService.insertCommodity(commodity));
    }


    /**
     * 修改商品
     */
    @RequiresPermissions("business:commodity:edit")
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long postId, ModelMap mmap) {
        mmap.put("customer", commodityService.selectCommodityById(postId));
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("business:commodity:edit")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam("file") MultipartFile file,
                               String coding,
                               String name,
                               String costPrice,
                               String sellingPrice,
                               String brand,
                               Long id) throws IOException {
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        Commodity commodity=new Commodity();
        commodity.setBrand(brand);
        commodity.setName(name);
        commodity.setCoding(coding);
        commodity.setPicture(fileName);
        commodity.setCostPrice(new BigDecimal(costPrice));
        commodity.setSellingPrice(new BigDecimal(sellingPrice));
        commodity.setId(id);
        System.out.println(commodity);
        return toAjax(commodityService.updateCommodity(commodity));
    }


}